//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CDynamicIndex9.cpp>
///	@path	~/src/graphics/dx9/
///	@date	2009/01/11
///	@desc	.

#include "config/config.h"

#include "graphics/dx9/CDynamicIndex9.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CDynamicIndex9 );
	
	// CDynamicIndex9
	CDynamicIndex9::CDynamicIndex9( IDirect3DIndexBuffer9 * buffer , size_t count )
		: CBaseIndex9( buffer , count )
	{
	}
	// ~CDynamicIndex9
	CDynamicIndex9::~CDynamicIndex9( void )
	{
	}
	// Map
	bool CDynamicIndex9::Map( bool discard , ResourceMap& data )
	{
		if( m_pBuffer == NULL )
			return false;

		void * pData = NULL;
		HRESULT hr = m_pBuffer->Lock( 0 , 0 , &pData , 0 );
		if( FAILED(hr) )
		{
			TRACE_ERROR( _S("CDynamicIndex9::Map: Failed to lock vertex buffer: %s") ,
				d3d_get_err(hr) );
			return false;
		}
		size_t size = (m_desc.Format == D3DFMT_INDEX16) ? sizeof(short) : sizeof(int32);
		data.Map( (byte*)pData , m_desc.Size , size , m_count );
		return true;
	}
	// ReadBack
	bool CDynamicIndex9::ReadBack( ResourceMap& data , bool readonly )
	{
		TRACE_ERROR( _S("CDynamicIndex9::ReadBack: Constant index is not lockable for read.") );
		return false;
	}
	// Unmap
	bool CDynamicIndex9::Unmap( void )
	{
		if( m_pBuffer == NULL )
			return false;

		m_pBuffer->Unlock();
		return true;
	}

} // namespace xeres
